package com.easy.web.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static boolean isEmpty(String str){
		return org.apache.commons.lang.StringUtils.isEmpty(str);
	}
	
	
	public static boolean isNotEmpty(String str){
		return org.apache.commons.lang.StringUtils.isNotEmpty(str);
	}
	
	
	public static String mapJoin(Map<String, ? extends Object> map){
		List<String> list = new ArrayList<String>();
		for(Map.Entry<String, ?> entry : map.entrySet()){
			list.add(entry.getKey() + "=" + entry.getValue());
		}
		return listJoin(list);
	}

	
	public static String listJoin(Collection<? extends Object> list){
		return listJoin(list, "&");
	}
	
	public static String arrayJoin(Object[] array){
		return arrayJoin(array, "&");
	}
	
	public static String mapJoin(Map<String, ? extends Object> map, String separator){
		List<String> list = new ArrayList<String>();
		for(Map.Entry<String, ?> entry : map.entrySet()){
			list.add(entry.getKey() + "=" + entry.getValue());
		}
		return listJoin(list, separator);
	}

	
	public static String listJoin(Collection<? extends Object> list, String separator){
		return org.apache.commons.lang.StringUtils.join(list, separator);
	}
	
	public static String arrayJoin(Object[] array, String separator){
		return org.apache.commons.lang.StringUtils.join(array, separator);
	}
	
	public static String capitalize(String str){
		if (str == null || str.length() == 0) {
			return str;
		}
		StringBuilder sb = new StringBuilder(str.length());
		sb.append(Character.toUpperCase(str.charAt(0)));
		sb.append(str.substring(1));
		return sb.toString();
	}
	
	/**
	 * 将字符串数组转化为byte二维数组
	 * @param strings
	 * @return
	 */
	public static byte[][] getBytesWithStringArray(String...strings){
		byte[][] bytes = new byte[strings.length][];
		for (int i = 0; i < strings.length; i++) {
			bytes[i] = strings[i].getBytes();
		}
		return bytes;
		
	}

	/**
	 * 判断字符串是否为正整数
	 */
	public static boolean isNumeric(String str){
		if(isEmpty(str)){
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if( !isNum.matches() ){
			return false;
		}
		return true;
	}

	public static void main(String[] args){
		System.out.println(isNumeric("9.3"));
	}

}
